package com.blog.controller;
import com.blog.exception.ExceptionEnum;
import com.blog.pojo.ResultResponse;
import com.blog.pojo.Tag;
import com.blog.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 标签管理控制层
@Slf4j
@RequestMapping("/tags")
@RestController
public class TagController {

    @Autowired
    private TagService tagService;

    // 查看所有标签
    @GetMapping
    public ResultResponse list() {
        log.info("查看所有标签");
        List<Tag> tagList = tagService.list();
        return ResultResponse.success(tagList);
    }

    // 添加标签
    @PostMapping("/create")
    public ResultResponse add(@RequestBody Tag tag) {
        log.info("添加标签");
        if(tag.getTagName() == null) {
            return ResultResponse.error(ExceptionEnum.EMPTY_TAGNAME);
        }
        tagService.add(tag);
        return ResultResponse.success();
    }

    // 根据id删除标签
    @DeleteMapping("/delete/{id}")
    public ResultResponse delete(@PathVariable Integer id)  {
        log.info("删除标签，id: {}", id);
        tagService.delete(id);
        return ResultResponse.success();
    }

}
